﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Store;
using System.Threading;
using System.Globalization;
using DTO;

public partial class ShowProduct : System.Web.UI.Page
{
    public string urlToolTip = "";
    private bool _userCanEdit = false;
    protected bool UserCanEdit
    {
        get { return _userCanEdit; }
        set { _userCanEdit = value; }
    }

    private string _titleProduct = "";
    public string TitleProduct
    {
        get { return _titleProduct; }
        set { _titleProduct = value; }
    }

    int _productID = 0;

    protected void Page_Init(object sender, EventArgs e)
    {
        UserCanEdit = (this.User.Identity.IsAuthenticated &&(this.User.IsInRole("Administrators")));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.Request.QueryString["ID"]))
            throw new ApplicationException("Missing parameter on the querystring.");
        else
            _productID = int.Parse(this.Request.QueryString["ID"]);

        if (!this.IsPostBack)
        {
            // try to load the product with the specified ID, and raise an exception if it doesn't exist
            Product product = Product.GetProductByID(_productID);
            if (product == null)
                throw new ApplicationException("No product was found for the specified ID.");
            
            product.IncrementViewCount();

            hplDatHang.NavigateUrl = Globals.ApplicationPath + "ShoppingCart.aspx?ID=" + _productID;

            this.Title = string.Format(this.Title, product.Title);
            TitleProduct = product.Title;
            lblTitle.Text = product.Title;
            lblLuotXem.Text = product.SKU.ToString();
            Panel1.Visible = (product.DiscountPercentage > 0);
            Panel2.Visible = (product.DiscountPercentage <= 0);
            lblGiaGoc.Text = product.StringUnitPrice;
            lblGiamgia.Text = product.FinalUnitPrice.ToString() + "%";
            lblGiaKM.Text = product.StringDiscountPercentage;
            lblGia.Text = product.StringUnitPrice;
            //lblOrgin.Text = product.Orgin;
            //lblCodeProduct.Text = product.CodeProduct;
            if (product.Description != "" || product.Description != null)
                lblDescription.Text = "<b>Thông tin chi tiết :</b></br>" + product.Description;
            panEditProduct.Visible = this.UserCanEdit;
            lnkEditProduct.NavigateUrl = string.Format(lnkEditProduct.NavigateUrl, _productID);
            //hplDepartmentID.Text = product.Department.Title;

            //lblPrice.Text = product.StringFinalUnitPrice ;
            //lblDiscountedPrice.Text = string.Format(lblDiscountedPrice.Text,
              // product.StringUnitPrice, product.DiscountPercentage);
            //lblDiscountedPrice.Visible = (product.DiscountPercentage > 0);
            //if (product.FullImageUrl.Length > 0)
            //    imgProduct.ImageUrl = product.FullImageUrl;
            //if (product.FullImageUrl.Length > 0)
            //    hplFullImage.NavigateUrl = product.FullImageUrl;
        }
    }
    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Write("Chua Xu Ly");
    }

    protected override void InitializeCulture()
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("vi-VN");
    }
}
